<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">权限名</label>
                    <div class="layui-input-block">
                        <input type="text" name="powerName"  autocomplete="off" placeholder="请输入权限名" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="powerType" value="0" title="目录" checked lay-filter="powerType">
                        <input type="radio" name="powerType" value="1" title="菜单" lay-filter="powerType">
                        <input type="radio" name="powerType" value="2" title="按钮" lay-filter="powerType">
                    </div>
                </div>
                <div class="layui-form-item" id="parentPerm">
                    <label class="layui-form-label">父级权限</label>
                    <div class="layui-input-block">
                        <ul id="powerSelectTree" class="dtree" data-id="0"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限标识</label>
                    <div class="layui-input-block">
                        <input type="text"  name="powerCode" autocomplete="off" placeholder="请输入权限标识" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限url</label>
                    <div class="layui-input-block">
                        <input type="text"  name="powerUrl" autocomplete="off" placeholder="请输入权限url" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限icon</label>
                    <div class="layui-input-block">
                        <input type="text"  name="icon" autocomplete="off" placeholder="请输入权限icon" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="text" lay-verify="required"  name="sort" autocomplete="off" placeholder="请输入排序" class="layui-input" onkeyup="this.value=this.value.replace(/\D/g, '')">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="perm-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
layui.use(['form','jquery','select','dtree'],function(){
    let form = layui.form;
    let $ = layui.jquery;
    let select = layui.select;
    let dtree = layui.dtree;
    form.on('submit(perm-save)', function(data){
        delete data.field.powerSelectTree_select_input;
        $.ajax({
            url:'/permission/create',
            data:JSON.stringify(data.field),
            dataType:'json',
            contentType:'application/json',
            type:'post',
            success:function(){
                layer.msg('权限新增成功',{icon:1,time:1000},function(){
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                    parent.window.refresh();
                });
            },
            error: function (data) {
                layer.msg(data.responseJSON.detail,{icon:2,time:1000});
            }
        })
        return false;
    });

    form.on('radio(powerType)', function (data) {
        dtree.reload("powerSelectTree", {
            url: "/perm-all"
        });
    });

    /**
     * 初始化下拉树
     */
    window.init = function () {
        var DTree = dtree.render({
            elem:"#powerSelectTree",
            dataStyle: "layuiStyle",
            response: {message: "msg", statusCode: 0},
            icon: ["0", "-1"],
            url: "/perm-all",
            select: true,
            selectInputName: {
                nodeId: "parentId"
            }
        });
    };
})
</script>
</body>
</html>
